package com.jf.exam.controller.manage;

import com.jf.exam.pojo.data.GradeDO;
import com.jf.exam.pojo.data.MajorDO;
import com.jf.exam.pojo.vo.ClassListVO;
import com.jf.exam.pojo.vo.ClassVO;
import com.jf.exam.pojo.vo.TeacherVO;
import com.jf.exam.service.ClassService;
import com.jf.exam.service.GradeService;
import com.jf.exam.service.MajorService;
import com.jf.exam.utils.PageBean;
import com.jf.exam.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 班级管理
 */
@Controller
@RequestMapping("/admin/clazz")
public class ManageClassController {

    //班级Service
    @Autowired
    ClassService clazzService;


    //年级Service
    @Autowired
    GradeService gradeService;

    //专业Service
    @Autowired
    MajorService majorService;

    @Value("#{properties['clazz.pageSize']}")
    private int pageSize;

    @Value("#{properties['clazz.navigatePages']}")
    private int navigatePages;

    /**
     * 班级列表
     */
    @RequestMapping("/list")
    public String list(ClassVO query, Model model) throws Exception {
        model.addAttribute("function", 4);
        query.setPageSize(pageSize);
        query.setSize(navigatePages);
        PageBean<ClassListVO> pageBean = clazzService.listClass(query);

        //查询年级列表
        List<GradeDO> grades = gradeService.listAll();

        //查询专业列表，给【添加班级】使用
        List<MajorDO> majors = majorService.listAll();

        model.addAttribute("pageBean", pageBean);
        model.addAttribute("grades", grades);
        model.addAttribute("majors", majors);
        //将查询封装的数据自己返回，用于查询回显
        model.addAttribute("query", query);
        return "admin/clazz_list";
    }

    /**
     * 添加班级
     */
    @RequestMapping("/add")
    @ResponseBody
    public Result add(Integer grade, Integer major, Integer clazz) throws Exception {
        ClassVO classVO = new ClassVO();
        classVO.setFkMajor(major);
        classVO.setFkGrade(grade);
        classVO.setCno(clazz);
        return clazzService.addClass(classVO);
    }

    /**
     * 通过id删除班级信息
     *
     * @param cid 班级id
     * @return 删除结果
     */
    @RequestMapping("/delete/{cid}")
    @ResponseBody
    public Result delete(@PathVariable Integer cid) throws Exception {
        ClassVO classVO = new ClassVO();
        classVO.setId(cid);
        return clazzService.deleteClass(classVO);
    }


    /**
     * 获取教师所教班级
     *
     * @param tid
     * @return
     */
    @RequestMapping("/listByTeacher")
    @ResponseBody
    public Result listByTeacher(String tid) {
        //根据教师id去查询所教的班级
        //拿教师的id去teacher_class表里面查询
        //SELECT * FROM class WHERE id IN(SELECT cid FROM teacher_class WHERE tid = '1111')
        TeacherVO teacherVO = new TeacherVO();
        teacherVO.setId(tid);
        return clazzService.listByTeacher(teacherVO);
    }


}
